package com.sl.hx.shop;

import android.app.Application;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.StrictMode;
import android.support.multidex.MultiDex;
import android.widget.Toast;

import com.alibaba.baichuan.android.trade.AlibcTradeSDK;
import com.alibaba.baichuan.android.trade.callback.AlibcTradeInitCallback;
import com.igexin.sdk.PushManager;
import com.igexin.sdk.PushService;
import com.sl.hx.shop.constant.AppConfig;
import com.sl.hx.shop.service.PushIntentService;
import com.umeng.commonsdk.UMConfigure;
import com.umeng.socialize.PlatformConfig;


/**
 * App
 * Created by wangfu on 2018/1/2.
 */

public class App extends Application {

    private static Context context;
    private static Application application;
    private static final String UMENG_CHANNEL = "UMENG_CHANNEL";


    @Override
    public void onCreate() {
        super.onCreate();
        context = getApplicationContext();
        application = this;
        //设置LOG开关，默认为false
        //UMConfigure.setLogEnabled(true);

        // com.getui.demo.DemoPushService 为第三方自定义推送服务
        PushManager.getInstance().initialize(this.getApplicationContext(), PushService.class);
        PushManager.getInstance().registerPushIntentService(this.getApplicationContext(), PushIntentService.class);

        UMConfigure.init(this, UMConfigure.DEVICE_TYPE_PHONE, null);

        try {
            ApplicationInfo appInfo = this.getPackageManager()
                    .getApplicationInfo(getPackageName(),
                            PackageManager.GET_META_DATA);
            AppConfig.CHANNEL = appInfo.metaData.getString(UMENG_CHANNEL);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }

        initAlibcTrade();

    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }

    private void initAlibcTrade() {
        AlibcTradeSDK.asyncInit(this, new AlibcTradeInitCallback() {
            @Override
            public void onSuccess() {
                //初始化成功，设置相关的全局配置参数
                //Toast.makeText(App.this, "初始化成功", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFailure(int code, String msg) {
                //初始化失败，可以根据code和msg判断失败原因，详情参见错误说明
                Toast.makeText(App.this, "初始化失败,错误码=" + code + " / 错误消息=" + msg, Toast.LENGTH_SHORT).show();

            }
        });
    }

    public static Context getContext() {
        return context;
    }

    public static Application getApplication(){
        return application;
    }

    {
        //友盟微信分享的配置,微信
        PlatformConfig.setWeixin("wxf7d420d0022f40e1", "2188a5350703c695a139437f24487699");
    }

}
